package com.fancy.learncenter.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.CartoonContructSatisticsesBean;
import com.fancy.learncenter.bean.CartoonQuestionBankBean;
import com.fancy.learncenter.bean.CartoonStudentStatisBean;
import com.fancy.learncenter.bean.QuestionVOsBean;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.ui.adapter.TeacherReportContentAdapter;
import com.fancy.learncenter.utils.FileUtils;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 作业报告--作业内容
 */
public class CartoonTeacherReportContentListActivity extends BaseActivity {

    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.right)
    TextView right;
    @Bind(R.id.right_img)
    ImageView rightImg;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.user_name_text)
    TextView userNameText;
    @Bind(R.id.complete_accurate)
    TextView completeAccurate;
    @Bind(R.id.complete_layout)
    LinearLayout completeLayout;
    @Bind(R.id.right_accurate)
    TextView rightAccurate;
    @Bind(R.id.right_layout)
    LinearLayout rightLayout;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    TeacherReportContentAdapter adapter;

    String commitCount;

    String studentHomeworkId;

    String studentName;
    String homePackageId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cartoon_teacher_report_content_list, Status.STATUS);
        ButterKnife.bind(this);
        initToolbar("作业内容");
        homePackageId = getIntent().getStringExtra("homePackageId");
        commitCount = getIntent().getStringExtra("commitCount");
        studentHomeworkId = getIntent().getStringExtra("studentHomeworkId");
        studentName = getIntent().getStringExtra("studentName");
        initView();

        getStaticsReport();

        downloadWork();
    }

    private void initView() {

        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        adapter = new TeacherReportContentAdapter(this, new ArrayList<CartoonContructSatisticsesBean>());
        adapter.setStudentName(studentName);
        recyclerView.setAdapter(adapter);
        adapter.setStudentHomeworkId(studentHomeworkId);

        completeAccurate.setText(commitCount);
        userNameText.setText(studentName);
    }

    /**
     * 获取作业报告-统计数据
     */
    private void getStaticsReport() {

        Map<String, Object> map = new HashMap<>();
        map.put("studentHomeworkId", studentHomeworkId);

        HttpMehtod.getInstance().cartoonStaticsReport(map, new CustomNetSubscriber(this) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {
                BaseBean<CartoonStudentStatisBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), CartoonStudentStatisBean.class);
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<CartoonStudentStatisBean>() {
                    @Override
                    public void success(CartoonStudentStatisBean dataBean) {
                        if (dataBean != null) {
                            if (!TextUtils.isEmpty(dataBean.getStudentHomeworktSatistics().getAccuracy())) {
                                rightAccurate.setText(dataBean.getStudentHomeworktSatistics().getAccuracy());
                                adapter.setAccuracy(dataBean.getStudentHomeworktSatistics().getAccuracy());
                            }
                            adapter.notifyDataSetChanged(dataBean.getStudentHomeworktSatistics().getHomeworkContructSatisticses());
                        }
                    }

                    @Override
                    public void fail(String errorCode) {

                    }
                }).dealData();
            }
        });

    }

    ArrayList<QuestionVOsBean> questionVOs;

    /**
     * 下载作业包
     *
     * @param
     */
    private void downloadWork() {
        Map<String, Object> map = new HashMap<>();
        map.put("homeworkPackageId", homePackageId);
        HttpMehtod.getInstance().cartoonTeacherDownLoadHomeworkById(map, new CustomNetSubscriber(this) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {

                BaseBean<CartoonQuestionBankBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), CartoonQuestionBankBean.class);
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<CartoonQuestionBankBean>() {
                    @Override
                    public void success(CartoonQuestionBankBean dataBean) {
                        questionVOs = dataBean.getQuestionVOs();
                        adapter.setQuestionVOs(dataBean.getQuestionVOs());
                    }

                    @Override
                    public void fail(String errorCode) {

                    }
                }).dealData();
            }
        });
    }


}
